<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="上报时间" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="reportTime">
              <j-date placeholder="请选择上报时间" v-model="model.reportTime"  :disabled="this.model.id" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="用能单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="workshopId">
  	          <j-tree-select
                ref="treeSelect"
                placeholder="请选择用能单位"
                v-model="model.workshopId"
                dict="ene_workshop,name,id"
                pidValue="0"
                hasChildField="has_child"
                :disabled="this.model.id"
                >
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="班次" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="scheduling">
              <j-popup
                v-model="model.scheduling"
                field="scheduling"
                org-fields="id,name"
                dest-fields="schedulingId,scheduling"
                code="ene_scheduling"
                :multi="false"
                :param="{worId:model.workshopId==null?'':model.workshopId.toString()}"
                @input="popupCallback"
                :disabled="this.model.id"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="energy">
              <j-popup
                v-model="model.energy"
                field="energy"
                org-fields="id,type,unit,input_method"
                dest-fields="energyId,energy,unit,inputMethod"
                code="ene_energy_type"
                :multi="false"
                @input="energyCallback"
                :disabled="this.model.id"
                />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="单位" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="unit">
              <a-input v-model="model.unit" placeholder="请输入单位" disabled ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">

            <a-form-model-item v-if ="inputMethod==''"  label="表数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="meter">
              <a-input-number v-model="model.meter" placeholder="请输入表数" style="width: 100%" disabled/>
            </a-form-model-item>
            <a-form-model-item v-else-if="inputMethod==0"  label="表数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="meter">
              <a-input-number v-model="model.meter" placeholder="请输入表数" style="width: 100%"/>
            </a-form-model-item>
            <a-form-model-item v-else label="用量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="consume">
              <a-input-number v-model="model.consume" placeholder="请输入用量" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
              <a-textarea v-model="model.remarks" rows="4" placeholder="请输入备注" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'EneTranscribeForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        inputMethod:'',
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           reportTime: [
              { required: true, message: '请输入上报时间!'},
           ],
           workshopId: [
              { required: true, message: '请输入用能单位!'},
           ],
           scheduling: [
              { required: true, message: '请输入班次!'},
           ],
           energy: [
              { required: true, message: '请输入类型!'},
           ],
           meter: [
              { required: true, message: '请输入表数!'},
           ],
          consume: [
            { required: true, message: '请输入用量!'},
          ],
        },
        url: {
          add: "/iot/eneTranscribe/add",
          edit: "/iot/eneTranscribe/edit",
          queryById: "/iot/eneTranscribe/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            if(this.inputMethod==0){
              this.model.consume=null
            }else if(this.inputMethod==1){
              this.model.meter=null
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
      popupCallback(value,row){
         this.model = Object.assign(this.model, row);
      },
      energyCallback(value,row){
        this.model = Object.assign(this.model, row);
        this.inputMethod=row.inputMethod
  /*      this.model.meter!=null?this.model.meter=null:""
        this.model.consume!=null?this.model.consume=null:""*/
      }
    }
  }
</script>